<template>
  <el-container class="layout-container">
    <el-aside :width="sidebarCollapse ? '64px' : '240px'" class="sidebar">
      <div class="logo-container">
        <img src="@/assets/logo.svg" alt="Logo" class="logo" v-if="!sidebarCollapse" />
        <div class="logo-text" v-if="!sidebarCollapse">小微资产管理系统</div>
      </div>
      
      <el-scrollbar class="scrollbar-container">
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          :collapse="sidebarCollapse"
          :router="true"
          :collapse-transition="false"
          background-color="#001529"
          text-color="#fff"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/dashboard">
            <el-icon><HomeFilled /></el-icon>
            <template #title>仪表盘</template>
          </el-menu-item>
          
          <el-sub-menu index="statistics">
            <template #title>
              <el-icon><DataAnalysis /></el-icon>
              <span>统计分析</span>
            </template>
            <el-menu-item index="/statistics/asset-overview">资产概览</el-menu-item>
            <el-menu-item index="/statistics/status-distribution">状态分布</el-menu-item>
            <el-menu-item index="/statistics/dept-distribution">部门分布</el-menu-item>
            <el-menu-item index="/statistics/asset-ledger">资产台账</el-menu-item>
            <el-menu-item index="/statistics/asset-transfer">资产流转</el-menu-item>
            <el-menu-item index="/statistics/dept-heat">部门领用热度</el-menu-item>
          </el-sub-menu>
          
          <el-sub-menu index="check">
            <template #title>
              <el-icon><Check /></el-icon>
              <span>资产盘点</span>
            </template>
            <el-menu-item index="/check/plan">盘点计划</el-menu-item>
            <el-menu-item index="/check/plan-settings">计划设置</el-menu-item>
            <el-menu-item index="/check/check-progress">盘点进度</el-menu-item>
            <el-menu-item index="/check/check-record">盘点记录</el-menu-item>
          </el-sub-menu>
          
          <el-menu-item index="/login">
            <el-icon><SwitchButton /></el-icon>
            <template #title>登录</template>
          </el-menu-item>
        </el-menu>
      </el-scrollbar>
      
      <div class="collapse-btn" @click="toggleSidebar">
        <el-icon :class="{ 'rotate-icon': sidebarCollapse }">
          <ArrowLeft v-if="!sidebarCollapse" />
          <ArrowRight v-else />
        </el-icon>
      </div>
    </el-aside>
    
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-for="(item, index) in breadcrumbItems" :key="index" :to="{ path: item.path }">
              {{ item.title }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar size="small" icon="el-icon-user" />
              <span class="user-name">{{ currentUser.name }}</span>
              <el-icon class="dropdown-icon"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="viewProfile">个人中心</el-dropdown-item>
                <el-dropdown-item @click="changePassword">修改密码</el-dropdown-item>
                <el-dropdown-item divided @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          
          <el-dropdown>
            <el-button size="small" plain>
              <el-icon><Bell /></el-icon>
              <span class="badge" v-if="notificationCount > 0">{{ notificationCount }}</span>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <div class="notification-header">
                  <span>通知消息</span>
                  <el-button type="text" size="small" @click="markAllAsRead">全部已读</el-button>
                </div>
                <div class="notification-list">
                  <el-dropdown-item v-for="notification in notifications" :key="notification.id" class="notification-item">
                    <div class="notification-content">
                      <div class="notification-title">{{ notification.title }}</div>
                      <div class="notification-desc">{{ notification.description }}</div>
                      <div class="notification-time">{{ formatTime(notification.time) }}</div>
                    </div>
                  </el-dropdown-item>
                  <el-dropdown-item class="view-all">
                    <el-button type="text" size="small" @click="viewAllNotifications">查看全部</el-button>
                  </el-dropdown-item>
                </div>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main class="main-content">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <keep-alive>
              <component :is="Component" />
            </keep-alive>
          </transition>
        </router-view>
      </el-main>
      
      <el-footer class="footer">
        <div class="footer-content">
          <span>© 2024 资产管理系统 - 版权所有</span>
        </div>
      </el-footer>
    </el-container>
  </el-container>
</template>

<script>
import { 
  HomeFilled, 
  DataAnalysis, 
  Check, 
  SwitchButton, 
  ArrowLeft, 
  ArrowRight,
  Bell
} from '@element-plus/icons-vue'

export default {
  name: 'Layout',
  components: {
    HomeFilled,
    DataAnalysis,
    Check,
    SwitchButton,
    ArrowLeft,
    ArrowRight,
    Bell
  },
  data() {
    return {
      sidebarCollapse: false,
      currentUser: {
        name: '管理员'
      },
      notificationCount: 3,
      notifications: [
        {
          id: 1,
          title: '新的盘点计划已创建',
          description: '2024年Q2季度盘点计划已创建，请及时查看',
          time: new Date('2024-03-15 09:30:00'),
          read: false
        },
        {
          id: 2,
          title: '资产即将到期',
          description: '您负责的3台设备保修期即将到期',
          time: new Date('2024-03-14 16:45:00'),
          read: false
        },
        {
          id: 3,
          title: '系统更新通知',
          description: '资产管理系统将于今晚22:00-24:00进行升级维护',
          time: new Date('2024-03-14 08:00:00'),
          read: true
        }
      ]
    }
  },
  computed: {
    activeMenu() {
      const path = this.$route.path
      return path || '/dashboard'
    },
    breadcrumbItems() {
      const path = this.$route.path
      const items = []
      
      if (path.includes('/statistics')) {
        items.push({
          title: '统计分析',
          path: '/statistics/asset-overview'
        })
        if (path.includes('/statistics/asset-overview')) {
          items.push({ title: '资产概览', path: '/statistics/asset-overview' })
        } else if (path.includes('/statistics/status-distribution')) {
          items.push({ title: '状态分布', path: '/statistics/status-distribution' })
        } else if (path.includes('/statistics/dept-distribution')) {
          items.push({ title: '部门分布', path: '/statistics/dept-distribution' })
        } else if (path.includes('/statistics/asset-ledger')) {
          items.push({ title: '资产台账', path: '/statistics/asset-ledger' })
        } else if (path.includes('/statistics/asset-transfer')) {
          items.push({ title: '资产流转', path: '/statistics/asset-transfer' })
        } else if (path.includes('/statistics/dept-heat')) {
          items.push({ title: '部门领用热度', path: '/statistics/dept-heat' })
        }
      } else if (path.includes('/check')) {
        items.push({
          title: '资产盘点',
          path: '/check/check-plan'
        })
        if (path.includes('/check/check-plan')) {
          items.push({ title: '盘点计划', path: '/check/check-plan' })
        } else if (path.includes('/check/check-plan-settings')) {
          items.push({ title: '计划设置', path: '/check/check-plan-settings' })
        } else if (path.includes('/check/check-progress')) {
          items.push({ title: '盘点进度', path: '/check/check-progress' })
        } else if (path.includes('/check/check-record')) {
          items.push({ title: '盘点记录', path: '/check/check-record' })
        }
      }
      
      return items
    }
  },
  methods: {
    toggleSidebar() {
      this.sidebarCollapse = !this.sidebarCollapse
    },
    formatTime(time) {
      const now = new Date()
      const notificationTime = new Date(time)
      const diff = now - notificationTime
      
      const minutes = Math.floor(diff / 60000)
      const hours = Math.floor(diff / 3600000)
      const days = Math.floor(diff / 86400000)
      
      if (minutes < 60) {
        return `${minutes}分钟前`
      } else if (hours < 24) {
        return `${hours}小时前`
      } else if (days < 7) {
        return `${days}天前`
      } else {
        return notificationTime.toLocaleDateString('zh-CN')
      }
    },
    viewProfile() {
      console.log('查看个人中心')
      // 跳转到个人中心页面
    },
    changePassword() {
      console.log('修改密码')
      // 打开修改密码对话框
    },
    logout() {
      this.$confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('退出登录')
        this.$router.push('/login')
      }).catch(() => {})
    },
    markAllAsRead() {
      this.notifications.forEach(notification => {
        notification.read = true
      })
      this.notificationCount = 0
      this.$message.success('已将全部通知标记为已读')
    },
    viewAllNotifications() {
      console.log('查看全部通知')
      // 跳转到通知列表页面
    }
  },
  mounted() {
    // 在组件挂载时，可以从localStorage或其他存储中恢复侧边栏状态
    const savedCollapseState = localStorage.getItem('sidebarCollapse')
    if (savedCollapseState !== null) {
      this.sidebarCollapse = savedCollapseState === 'true'
    }
  },
  watch: {
    sidebarCollapse(newVal) {
      // 保存侧边栏状态到localStorage
      localStorage.setItem('sidebarCollapse', newVal)
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  background-color: #001529;
  color: #fff;
  position: relative;
  transition: width 0.3s ease;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px 0;
  border-bottom: 1px solid #1f2d3d;
}

.logo {
  height: 32px;
  margin-right: 10px;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  color: #fff;
}

.scrollbar-container {
  height: calc(100% - 130px);
}

.collapse-btn {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: #1f2d3d;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.collapse-btn:hover {
  background-color: #409EFF;
}

.rotate-icon {
  transform: rotate(180deg);
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-name {
  margin-left: 8px;
  font-size: 14px;
}

.dropdown-icon {
  margin-left: 5px;
}

.badge {
  position: absolute;
  top: -5px;
  right: -5px;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  background-color: #f56c6c;
  color: #fff;
  font-size: 12px;
  text-align: center;
  line-height: 18px;
  padding: 0 5px;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  border-bottom: 1px solid #e6e6e6;
}

.notification-list {
  max-height: 400px;
  overflow-y: auto;
}

.notification-item {
  padding: 10px 15px;
  cursor: pointer;
}

.notification-item:hover {
  background-color: #f5f7fa;
}

.notification-content {
  min-width: 300px;
}

.notification-title {
  font-weight: bold;
  margin-bottom: 5px;
}

.notification-desc {
  font-size: 13px;
  color: #606266;
  margin-bottom: 5px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.notification-time {
  font-size: 12px;
  color: #909399;
}

.view-all {
  text-align: center;
  border-top: 1px solid #e6e6e6;
  padding: 8px 0;
}

.main-content {
  padding: 20px;
  overflow-y: auto;
  background-color: #f5f7fa;
}

.footer {
  background-color: #fff;
  border-top: 1px solid #e6e6e6;
  padding: 0;
}

.footer-content {
  text-align: center;
  padding: 10px 0;
  color: #606266;
  font-size: 14px;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>

<style>
/* 全局样式 */
body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: #303133;
  background-color: #f5f7fa;
}

/* 滚动条样式 */
.el-scrollbar__wrap {
  overflow-x: hidden !important;
}

::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>